package com.squirrels.server.work;

import com.squirrels.server.annotation.NettyServerWorker;
import com.squirrels.server.common.constant.SystemConstant;
import com.squirrels.server.common.constant.UserConstant;
import com.squirrels.server.message.Login;
import com.squirrels.server.message.protobuf.Message;
import com.squirrels.server.service.UserService;
import com.squirrels.server.utils.EntityUtils;
import com.squirrels.server.utils.JWTUtils;
import dev.morphia.Datastore;
import io.netty.channel.socket.DatagramPacket;
import lombok.extern.slf4j.Slf4j;

/**
 * 创建者: FlyingSquirrels
 * 创建时间: 2024/7/30 10:48
 * 描述: 登录功能
 */
@Slf4j
@NettyServerWorker("connect")
public class NetWorker extends ServerWorker{

    private static UserService userService;

    /**
     * 为当前类注入service
     * @param datastore
     */
    private void doService(Datastore datastore) {
        userService = UserService.operation(datastore);
    }

    @Override
    public Message.Entity doWorker(String operationBase) {
        // 注入 Service
        doService(datastore);
        Message.Entity responseData;
        // 根据请求头部执行操作
        switch (operationBase){
            // 登录
            case "online" -> {
                responseData = online(entity);
            }
            default -> {
                responseData = errorMsg(UserConstant.OPERATION_NOT_ERROR,entity);
            }
        }
        return responseData;
    }

    // 服务在线响应
    private Message.Entity online(Message.Entity entity) {
        return EntityUtils.buildResData(entity, SystemConstant.ONLINE, null);
    }

}
